package com.zhao.savepassword;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {
    protected static final String KEY_PASSWORD = "app_pw";
    protected static final String SHARED_PREFERENCES_NAME="userInfo";
    private SharedPreferences mSharedPreferences;
    //登陆密码
    private String mPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        //使用SharedPreferences 读取设置的密码 如果没有设置 则 进入设置密码界面
        mSharedPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        mPassword = mSharedPreferences.getString(KEY_PASSWORD,null);


        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment= fm.findFragmentById(R.id.fl_main);
        if (fragment == null){
            if (mPassword!=null){
                fragment=new LogInFragment();
                Bundle bundle = new Bundle();
                bundle.putString(KEY_PASSWORD,mPassword);
                fragment.setArguments(bundle);
            }else{
                fragment = new SignInFragment();
            }
            fm.beginTransaction()
                    .add(R.id.fl_main,fragment)
                    .commit();
        }
    }
}
